<?php
    include("include/header.inc.php");
    include("include/func.inc.php");
    $_SESSION['page'] = 'event';
    
    //Function to display message when event not found
    function event_not_found($texts){
        echo '<p align="center">'.$texts['event_not_found'][$_SESSION['lang']].'<br/><br/>';
        echo '<a href="./events.php" rel="external" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
    }
    
    //Function to display content if not theme found
    function display_theme_not_found($texts){
        echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
        echo '<div class="tableauPage">';
        // Only the "event" parameter exists in the url
        if(isset ($_GET['event'])){
            //Error : theme not found
            echo '<p align="center">'.$texts['theme_not_found'][$_SESSION['lang']].'<br/><br/>';
            echo '<a href="./detailEvent.php?event='.$_GET['event'].'" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
        }
        else {
            //Error : event not found
             event_not_found($texts);
        }
        echo '</p></div>';
    }
    
    echo'<div data-role="content">';
    
    //Check that the "theme" parameter exist in the url
    if(isset ($_GET['theme'])) {
        
        //Current theme id
        $id_theme = $_GET['theme'];
        
        //SAP connection
        $rfc = connectSAP();

        //-- FUNCTION MODULE: Z_FM_M646_GET_THEMES --   
        //We must know if the function really exists
        $fce_get_themes = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
        if(!$fce_get_themes){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_get_themes,"IV_THEME",$id_theme);

        //Pass table parameters
        saprfc_table_init ($fce_get_themes,"TAB_DETAILS_THEME");

        //Call and execute the function
        $rc_get_themes = saprfc_call_and_receive ($fce_get_themes);            
        if ($rc_get_themes != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_get_themes));
            else
                echo ("Call error: ".saprfc_error($fce_get_themes));
            exit;
        }

        //Fetch the data from the internal tables
        $data_row = saprfc_table_rows ($fce_get_themes,"TAB_DETAILS_THEME");
        if ($data_row > 0){//If the theme exist

//echo'<p><a href="./unregisterValidate.php?theme='.$id_theme.'&event='.$_GET['event'].'" data-role="button" data-inline="true">Valider</a><a href="./detailTheme.php?theme='.$id_theme.'&event='.$_GET['event'].'" data-role="button" data-inline="true">Annuler</a></p>';

            $DATARow = saprfc_table_read ($fce_get_themes,"TAB_DETAILS_THEME",1);
              
            //Current theme id
            $id_event = htmlentities($DATARow['EVENT_ID']);

            //Variables to display the date in a correct format
            $dateStart = substr($DATARow['STARTDATE'],-2).'.'.substr($DATARow['STARTDATE'],4,2).'.'.substr($DATARow['STARTDATE'],0,4);
            $dateEnd = substr($DATARow['ENDDATE'],-2).'.'.substr($DATARow['ENDDATE'],4,2).'.'.substr($DATARow['ENDDATE'],0,4); 

            //Title
            echo '<h2>'.$texts['unregistered_to_theme'][$_SESSION['lang']].'</h2>';
            echo '<div class="tableauPage">';
            echo '<h3>'.htmlentities($DATARow['TITLE_THEME']).'</h3>';
            echo '<div style="margin-left:10px;">';
            //Display dates (one or more days)
            echo '<p>';
            if($dateStart != $dateEnd )
                echo $texts['from'][$_SESSION['lang']] .' '. $dateStart .' '. $texts['to'][$_SESSION['lang']] .' '. $dateEnd;
            else
                echo $dateStart;
            echo '<br/><br/>';
            echo htmlentities($DATARow['ADDRESS']).'<br/>';
            //Information
            echo $DATARow['ZIPCODE'].' '.htmlentities($DATARow['CITY']).'<br/>';
            //Language
            echo $texts['language'][$_SESSION['lang']].' : '.$DATARow['ID_LANGUAGE'].'<br/></p>';
            echo '</div>';
            echo '<p>'. $texts['for'][$_SESSION['lang']] .' : <b>'.$_SESSION['name'].'</b></p>';
            echo '<p><b>'.$texts['unregisterPreRequisPart1'][$_SESSION['lang']].htmlentities($DATARow['TITLE_THEME']).$texts['unregisterPreRequisPart2'][$_SESSION['lang']].'</b></p>';
            //Unsubscribe button
            echo'<p><a href="./unregisterValidate.php?theme='.$id_theme.'&event='.$id_event.'" data-role="button" data-inline="true">'. $texts['validate'][$_SESSION['lang']] .'</a>';
            //Display cancel button
            echo '<a href="./detailTheme.php?theme='.$id_theme.'&event='.$id_event.'" data-role="button" data-inline="true">'. $texts['cancel'][$_SESSION['lang']] .'</a></p>';
            echo '</div>';
        }
        else//These theme does not exist
            display_theme_not_found($texts);

        //realease the function and close the connection
        saprfc_function_free($fce_get_themes);

        saprfc_close($rfc);
    }
    else//The parameter "themes" does not exist
        display_theme_not_found($texts);
    
    echo '</div>';
    include('include/footer.inc.php');  // Call footer
?>